package com.ymt.fmd.platform.web.rest;

import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ymt.fmd.platform.service.BoService;
import com.ymt.fmd.util.Const;

@Component(value="fmdBoResource")
@Path("/bo")
public class BoResource {
	
	private Logger logger = Logger.getLogger(BoResource.class.getName());
	
	@Autowired
	private BoService bos;
	
	@Context
	private HttpServletRequest request;

	@GET
    @Path("/{boid}")
    @Produces(MediaType.APPLICATION_JSON)
	public String get(@PathParam("boid") String boid, 
			@QueryParam("check")boolean check, 
			@QueryParam("timezoneOffset")int timezoneOffset) {
		Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
		logger.finer("get boid="+boid);
		logger.finer("get check="+check);
		logger.finer("get timezoneOffset="+timezoneOffset);
		String lang = userDetail.get(Const.LANG);
		return bos.get(boid, check, lang, timezoneOffset);
	}
	
	@GET
    @Path("/getByName/{boname}")
    @Produces(MediaType.APPLICATION_JSON)
	public String getByName(@PathParam("boname") String boname, 
			@QueryParam("timezoneOffset")int timezoneOffset) {
		Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
		logger.finer("get boname="+boname);
		logger.finer("get timezoneOffset="+timezoneOffset);
		String lang = userDetail.get(Const.LANG);
		String tenantId = userDetail.get(Const.TENANT_ID);
		return bos.getByName(tenantId, boname, lang, timezoneOffset);
	}
	
	@POST
    @Path("/save")
    @Produces(MediaType.APPLICATION_JSON)
	public String save(@FormParam("bostr") String bostr) {
		Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
		logger.finer("get bostr="+bostr);
		String lang = userDetail.get(Const.LANG);
		String loginName = userDetail.get(Const.LOGINNAME);
		String displayName = userDetail.get(Const.DISPLAYNAME);
		String tenantId = userDetail.get(Const.TENANT_ID);
		String boid = bos.save(tenantId, bostr, loginName, displayName, lang);
		return "{\"result\":true, \"boid\":\""+boid+"\"}";
	}
	
	@DELETE
    @Path("/{boid}")
    @Produces(MediaType.APPLICATION_JSON)
	public String delete(@PathParam("boid") String boid) {
		logger.finer("delete boid="+boid);
		int rs = bos.delete(boid);
		return "{\"result\":"+rs+"}";
	}
	
	@POST
    @Path("/query")
    @Produces(MediaType.APPLICATION_JSON)
	public String query(@FormParam("queryParams")String queryParams,
			@FormParam("fromRow")int fromRow,
			@FormParam("toRow")int toRow,
			@FormParam("timezoneOffset")int timezoneOffset,
			@FormParam("ordercolumns")String ordercolumns) {
		Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
		String lang = userDetail.get(Const.LANG);
		String tenantId = userDetail.get(Const.TENANT_ID);
		logger.finer("query queryParams="+queryParams);
		logger.finer("query lang="+lang);
		logger.finer("query tenantId="+tenantId);
		return bos.query(tenantId, queryParams, fromRow, toRow, timezoneOffset, lang, ordercolumns);
	}
	
	@POST
    @Path("/attach")
    @Produces(MediaType.APPLICATION_JSON)
	public String attach(@FormParam("boid") String boid, @FormParam("checkOnly") String checkOnly) {
		logger.finer("attach boid="+boid);
		return bos.attach(boid, Boolean.TRUE.toString().equalsIgnoreCase(checkOnly));
	}
	
	@GET
    @Path("/isNameExists")
    @Produces(MediaType.APPLICATION_JSON)
	public String isNameExists(@QueryParam("boname") String boname, @QueryParam("boid") String boid) {
		Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
		logger.finer("isNameExists boname="+boname);
		logger.finer("isNameExists boid="+boid);
		String tenantId = userDetail.get(Const.TENANT_ID);
		return bos.isNameExists(tenantId, boname, boid);
	}
	
	
	@POST
	@Path("/getBoDatas")
	@Produces(MediaType.APPLICATION_JSON)
	public String getBoDatabyModal(@Context HttpServletRequest request,@FormParam("data") String params){
		Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
		// 语言(暂未使用)
		String lang = userDetail.get(Const.LANG);
		String rs = "";
		
		try {
			rs = bos.getBoDatabyModal(lang, params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return rs;
	}
	
	
	@POST
	@Path("/getsubBoDatas")
	@Produces(MediaType.APPLICATION_JSON)
	public String getBoSubDatabyModal(@Context HttpServletRequest request,@FormParam("data") String params){
		Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
		// 语言(暂未使用)
		String lang = userDetail.get(Const.LANG);
		String rs = "";
		
		try {
			rs = bos.getBoSubDatabyModal(lang,params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return rs;
	}
	
	@POST
    @Path("/getBdDict")
	@Produces(MediaType.APPLICATION_JSON)
	public String getBdDict(@FormParam("meta") String meta) {
		Map params = (Map) JSONObject.toJavaObject(JSONObject.parseObject(meta), Map.class);
		logger.finer("=========getBdDict==============meta======" + meta);
		if(meta!=null){
			List<Map> map = bos.getBdDict(params);
			JSONObject rtnJo = new JSONObject();
			if (null != map && map.size() > 0) {
				rtnJo.put("data", JSONArray.toJSONString(map));
				return rtnJo.toString();
			}
		}
		return "";
	}
	
	
	@POST
	@Path("/getOnesubBoData")
	@Produces(MediaType.APPLICATION_JSON)
	public String getOnesubBoData(@Context HttpServletRequest request,@FormParam("data") String params){
		Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
		// 语言(暂未使用)
		String lang = userDetail.get(Const.LANG);
		String rs = "";
		
		try {
			rs = bos.getOnesubBoData(lang,params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return rs;
	}
	
}
